package com.commons.web.controller;

import com.commons.service.dto.*;
import com.commons.service.service.*;
import com.commons.tools.utils.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.*;

import javax.servlet.http.*;
import java.util.*;

@Api(tags = "File Manage Controller")
@RestController
@RequestMapping("/files")
public class FileManageController {
    @Autowired
    private FileService fileService;

    @ApiOperation("文件上传接口")
    @PostMapping
    public Result<String> upload(@ApiParam("文件") @RequestParam("file") MultipartFile file) {
        return Result.ok(fileService.uploadFile(file));
    }

    @PostMapping(value = "/multipleFilesUpload", headers = "content-type=multipart/form-data")
    @ApiOperation("多文件上传")
    public Result<List<String>> multipleFilesUpload(@RequestPart("files") MultipartFile[] files) {
        return Result.ok(fileService.multipleFilesUpload(files));
    }

    @ApiOperation("文件下载接口")
    @GetMapping("/fileDown/{id}")
    public void download(@PathVariable(value = "id") Long fileId,
                         HttpServletRequest request, HttpServletResponse response) {
        fileService.downloadFile(fileId, request, response);
    }

    @ApiOperation("删除文件接口")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable long id) {
        return fileService.deleteFile(id);
    }


    @ApiOperation("导出表格数据为文件")
    @GetMapping("/export/{tableName}/{tableId}")
    public void exportTableAsExcel(@RequestBody PageDto pageDto,HttpServletResponse response) {
        fileService.exportTableAsExcel(response, pageDto);
    }

    @ApiOperation("导入表格数据到数据库")
    @PostMapping("/import")
    public void readExcel(MultipartFile file) {
         fileService.readExcel(file);
    }



}
